package com.example.meitao.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioGroup;

import com.example.meitao.R;
import com.example.meitao.fragment.AllFragment;
import com.example.meitao.fragment.WaitReceiveFragment;
import com.example.meitao.fragment.WaitSendFragment;
import com.example.meitao.fragment.WaitSpendFragment;

public class OrderActivity extends AppCompatActivity {

    //获取fragment管理器
    private FragmentManager mManager;
    //获取控件
    private RadioGroup mRadioGroup;
    //获得当前fragment
    Fragment mCurrentfragment ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        initView();
        initData();
        setData();
        setListener();
    }

    private void setData() {
       /* Intent intent = getIntent();
        String value = intent.getStringExtra("ischecked");
        Log.d("TAG", "showAndHide: ------ischecked="+value);
        if(value.equals("1")){
            showAndHide(R.id.fl_order_show,WaitSpendFragment.class);
        }else if(value.equals("2")){
            showAndHide(R.id.fl_order_show,WaitSendFragment.class);

        }else if(value.equals("3")){
            showAndHide(R.id.fl_order_show,WaitReceiveFragment.class);
        }*/
    }

    private void setListener() {
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                //根据不同的id获取不停的fragment
                Class<? extends Fragment> clazz =null;
                switch (i) {
                    case  R.id.rb_order_all:
                        clazz =AllFragment.class;
                        break;
                    case R.id.rb_order_waiting_spend:
                        clazz =WaitSpendFragment.class;
                        break;
                    case R.id.rb_order_waiting_send:
                        clazz =WaitSendFragment.class;
                        break;
                    case R.id.rb_order_waiting_receive:
                        clazz =WaitReceiveFragment.class;
                        break;
                }
                //获取显示和隐藏frahment
                showAndHide(R.id.fl_order_show,clazz);
            }
        });
    }

    private void showAndHide(int contentId, Class<? extends Fragment> clazz) {

        //当前fragment不为空
        if(mCurrentfragment!=null&&mCurrentfragment.getClass().getSimpleName().equals(clazz.getCanonicalName())){
            return;
        }
        FragmentTransaction transaction = mManager.beginTransaction();
        Fragment fragment =null;

        try {

            Fragment fragmentByTag = mManager.findFragmentByTag(clazz.getSimpleName());

            if(fragmentByTag!=null){
                //显示需要显示的fragment
                transaction.show(fragmentByTag);
                //隐藏当前的fragment
                transaction.hide(mCurrentfragment);
                //记录当前的fragment
                mCurrentfragment =fragmentByTag;
            }else{

                fragment =clazz.newInstance();
                transaction.add(contentId,fragment,clazz.getSimpleName());
                //隐藏当前的fragment
                if(mCurrentfragment!=null){
                    transaction.hide(mCurrentfragment);
                }
                mCurrentfragment =fragment;
            }
            transaction.commit();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    private void initData() {
        mManager =getSupportFragmentManager();
        //设置默认显示的fragemnt
        showAndHide(R.id.fl_order_show,AllFragment.class);
    }

    private void initView() {
        ActionBar supportActionBar = getSupportActionBar();
        if(supportActionBar!=null){
            supportActionBar.hide();
        }
        mRadioGroup = (RadioGroup) findViewById(R.id.rg_order_index);
    }
}
